export default class ToolBox {
    private static Instance: ToolBox = null;

    public static getInstance() {
        if (ToolBox.Instance == null) {
            ToolBox.Instance = new ToolBox();
        }

        return ToolBox.Instance;
    }

    /**
     * 获取所有的节点
     * @param root 根节点
     * @param viewMap 容器
     * @param path 无需传递
     * @returns 
     */
    public initViewMap(root: cc.Node, viewMap: Map<string, cc.Node>, path: string = '') {
        if (root == null || viewMap == null) {
            return;
        }

        for (let i = 0, j = root.childrenCount; i < j; i++) {
            viewMap.set(path + root.children[i].name, root.children[i]);
            this.initViewMap(root.children[i], viewMap, path + root.children[i].name + "/");
        }
    }

    public addBtnEvent(_btn: cc.Button, _target: cc.Node, _component: string, _handler: string, _customEventData: string) {
        if (_btn == null || _target == null) {
            return
        }
        var clickEventHandler = new cc.Component.EventHandler();
        clickEventHandler.target = _target;
        clickEventHandler.component = _component;               //这个是脚本文件名
        clickEventHandler.handler = _handler;                   //回调函名称
        clickEventHandler.customEventData = _customEventData;
        _btn.clickEvents.length = 0;                            //清除该按钮之前的事件
        _btn.clickEvents.push(clickEventHandler);
    }

}
